﻿using System.Text;
using System.Xml.XPath;

namespace GianosNET.Common.Classes
{
    public static class NavigatorExt
    {
        /// <summary>
        /// get Value string from navigator
        /// </summary>
        /// <param name="nav"></param>
        /// <param name="xpath"></param>
        /// <returns></returns>
        public static string GetData(this XPathNavigator nav, string xpath)
        {
            XPathNavigator tmp = nav.SelectSingleNode(xpath);
            if (tmp == null)
                return "";

            return tmp.Value ?? "";
        }

        public static string GetCompositeData(this XPathNavigator nav, string xpath)
        {
            StringBuilder sb = new StringBuilder();
            foreach (XPathNavigator navigator in nav.GetEnumerator(xpath))
                sb.Append(navigator.Value + " ");

            return sb.ToString();
        }

        public static XPathNodeIterator GetEnumerator(this XPathNavigator nav, string xpath)
        {
            return nav.Select(xpath);
        }

    }
}
